package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.constants.ConstMessages;
import com.jinzhi.common.constants.MessagesEnum;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.ShiroUtils;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.dao.JazOrderTakingDao;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.domain.param.UserInterViewBackGroundQuery;
import com.jinzhi.jzweb.domain.param.UserInterViewQuery;
import com.jinzhi.jzweb.domain.vo.userAunt.UserVO;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sys.dao.UserDao;
import com.jinzhi.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazInterviewDao;
import com.jinzhi.common.base.CoreServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 面试邀约表
 * </pre>
 * <small> 2024-05-13 15:29:16 | Long</small>
 */
@Service
public class JazInterviewServiceImpl extends CoreServiceImpl<JazInterviewDao, JazInterviewDO> implements JazInterviewService {

    @Autowired
    private JazSysProjectService jazSysProjectService;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserService userService;

    @Autowired
    private JazUserAuntService jazUserAuntService;

    @Autowired
    private JazMessageService jazMessageService;

    @Autowired
    private JazInterviewDao jazInterviewDao;

    @Autowired
    private JazUserDemandService jazUserDemandService;

    @Autowired
    private JazOrderTakingService jazOrderTakingService;

    /**
     * 后台-分页查询面试邀约列表
     *
     * @param userInterViewQuery
     * @return
     */
    @Override
    public Page<JazInterviewDO> getUserInterviewPage(UserInterViewQuery userInterViewQuery) {
        Page<JazInterviewDO> page = new Page<>(userInterViewQuery.getPageNumber(),userInterViewQuery.getPageSize());
        // 分页查询数据
        List<JazInterviewDO> jazUserInterview =  jazInterviewDao.selectInterviewPage(page,userInterViewQuery);

        // 查询被邀约人信息
        List<Long> userInviteeIds = jazUserInterview.stream().filter(a -> Objects.nonNull(a.getInviteeId())).
                map(JazInterviewDO::getInviteeId).distinct().collect(Collectors.toList());
        if(ObjectUtils.isEmpty(userInviteeIds)){
            return page;
        }
        // 查询用户表
        List<UserDO> userInviteeDOS = userDao.selectList(new EntityWrapper<UserDO>().in("id",userInviteeIds));
        //用户信息不等于null
        if(!ObjectUtils.isEmpty(userInviteeDOS)){
            jazUserInterview.forEach(a -> userInviteeDOS.forEach(b ->{
                if(Objects.equals(a.getInviteeId(),b.getId())){
                    a.setInviteeName(b.getName());
                    a.setInviteePhone(b.getOrderPhone());
                }
            }));
        }

        //查询项目类型
        List<String> projectIds = jazUserInterview.stream().filter(a ->Objects.nonNull(a.getProjectId()))
                .map(JazInterviewDO::getProjectId).distinct().collect(Collectors.toList());
        if(ObjectUtils.isEmpty(projectIds)){
            return page;
        }
        List<String> projectIds1 = new ArrayList<>();
        for (int i = 0; i < projectIds.size(); i++) {
            projectIds1.addAll(Arrays.asList(projectIds.get(i).split(",")));
        }
        List<JazSysProjectDO>  jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id",projectIds1));
        jazUserInterview.forEach(a-> {
            a.setProjectName(jazSysProjectDOS.stream().filter(b -> Objects.nonNull(a.getProjectId())).
                    filter(b -> a.getProjectId().contains(b.getId().toString())).
                    map(JazSysProjectDO::getName).collect(Collectors.joining(",")));
        });

        //如果根据姓名查询查询
        if (StringUtil.notEmpty(userInterViewQuery.getInviterName())) {
            String inviterNameToSearch = userInterViewQuery.getInviterName();
            jazUserInterview = jazUserInterview.stream()
                    .filter(a -> Objects.nonNull(a.getInviterName()) && a.getInviterName().contains(inviterNameToSearch))
                    .collect(Collectors.toList());
        }

        // 拼接用户找阿姨的标题
        jazUserInterview.stream().forEach(a->{
            if (a.getInterviewSource()==1){
                a.setTitle(a.getProjectName()+"-"+a.getWorkContent());
            }
        });



        // 设置分页信息
        page.setRecords(jazUserInterview);

        return page;
    }

    /**
     * 后台 根据id查询详情
     *
     * @param id
     * @return
     */
    @Override
    public JazInterviewDO getUserInterviewById(Long id) {
        if (id == null){
            return null;
        }
        // 查询面试信息
        JazInterviewDO jazInterviewDO = selectById(id);
        if (jazInterviewDO == null){
            return null;
        }
        // 判断面试方式显示面试地址
        if (jazInterviewDO.getInterviewFormat()==1){
            jazInterviewDO.setInterviewAddress(null);
        }
        // 判断面试邀约的来源方式  1 用户找阿姨 2 阿姨根据需求进行面试
        if (jazInterviewDO.getInterviewSource()==1){
            // 根据面试信息封装邀约人信息
            if (jazInterviewDO.getInviterId()!=null){
                UserDO userDO = userDao.selectById(jazInterviewDO.getInviterId());
                if (userDO!=null){
                    jazInterviewDO.setInviterName(userDO.getNickname());
                    jazInterviewDO.setInviterPhone(userDO.getMobile());
                }
            }
            // 根据面试信息封装被邀约人信息
            getInviteeInfo(jazInterviewDO);
        } else if (jazInterviewDO.getInterviewSource()==2) { //2 阿姨根据需求进行面试
            if (jazInterviewDO.getDemandId()==null){
                return null;
            }
            // 根据需求id 查询邀约人信息
            JazUserDemandDO demandDO = jazUserDemandService.selectById(jazInterviewDO.getDemandId());
            if (demandDO!=null){
               jazInterviewDO.setInviterName(demandDO.getUserName());
               jazInterviewDO.setInviterPhone(demandDO.getOrderPhone());
               jazInterviewDO.setSex(Integer.parseInt(demandDO.getSex()));
            }
            // 根据面试信息封装被邀约人信息
            getInviteeInfo(jazInterviewDO);
        }

        // 拼接面试地址
        if (StringUtil.notEmpty(jazInterviewDO.getInterviewAddressDetail())){
            StringBuffer append = new StringBuffer().append(jazInterviewDO.getInterviewAddress()).append(jazInterviewDO.getInterviewAddressDetail());
            jazInterviewDO.setInterviewAddress(append.toString());
        }

        //查询邀约人项目类型
        if(StringUtil.notEmpty(jazInterviewDO.getProjectId())){
            String[] projects = jazInterviewDO.getProjectId().split(",");
            List<JazSysProjectDO>  jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id",projects));
            jazInterviewDO.setProjectName(jazSysProjectDOS.stream().map(JazSysProjectDO::getName).collect(Collectors.joining(",")));
        }
        return jazInterviewDO;
    }

    /**
     * 获取邀约人信息
     * @param jazInterviewDO
     */
    private void getInviteeInfo(JazInterviewDO jazInterviewDO) {
        if (jazInterviewDO.getInviteeId()!=null){
            UserDO userDO = userDao.selectById(jazInterviewDO.getInviteeId());
            if (userDO!=null){
                jazInterviewDO.setInviteeName(userDO.getName());
                jazInterviewDO.setInviteePhone(userDO.getMobile());
                jazInterviewDO.setAge(userDO.getAge());
                jazInterviewDO.setEducation(userDO.getEducation());
                jazInterviewDO.setInviteeSex(userDO.getSex().intValue());
            }
            JazUserAuntDO jazUserAuntDO = jazUserAuntService.selectOne(new EntityWrapper<JazUserAuntDO>().eq("sys_userid", jazInterviewDO.getInviteeId()));
            if (jazUserAuntDO!=null){
                jazInterviewDO.setWorkExperience(jazUserAuntDO.getWorkExperience());
                jazInterviewDO.setExperience(jazUserAuntDO.getExperience());
                jazInterviewDO.setInchPhoto(jazUserAuntDO.getInchPhoto());

                //查询被邀约人项目类型
                if(StringUtil.notEmpty(jazUserAuntDO.getProjectId())){
                    String[] projects = jazUserAuntDO.getProjectId().split(",");
                    List<JazSysProjectDO>  jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id",projects));
                    jazInterviewDO.setInviteeProjectName(jazSysProjectDOS.stream().map(JazSysProjectDO::getName).collect(Collectors.joining(",")));
                }
            }
        }
    }

    /**
     * 面试邀约-确认推送
     *
     * @param jazInterviewDO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result userInterviewPush(JazInterviewDO jazInterviewDO) {
        if (jazInterviewDO==null){
            return null;
        }
        // 获取已推送的id
        JazInterviewDO interviewDO = this.selectById(jazInterviewDO.getId());
        if (jazInterviewDO!=null){
            StringBuffer buffer=new StringBuffer();
            String pushUserId = interviewDO.getPushUserId();
            if (StringUtil.notEmpty(pushUserId)){
                buffer.append(pushUserId).append(",").append(jazInterviewDO.getInviteeId());
                jazInterviewDO.setPushUserId(buffer.toString());
            }else {
                jazInterviewDO.setPushUserId(jazInterviewDO.getInviteeId().toString());
            }
        }
        if (interviewDO==null){
            return Result.fail();
        }

        interviewDO.setStatus(1);
        interviewDO.setResult(0);
        interviewDO.setInviteeId(jazInterviewDO.getInviteeId());
        interviewDO.setCancelReason("");
        interviewDO.setInterStatus(0);
        interviewDO.setUpdateTime(new Date());
        boolean update = this.updateById(interviewDO);

        // 查询月嫂id
        JazUserAuntDO jazUserAuntDO = jazUserAuntService.selectOne(new EntityWrapper<JazUserAuntDO>().eq("sys_userid", interviewDO.getInviteeId()));
        if (jazUserAuntDO == null){
            return Result.fail();
        }

        // 发送消息
        if (interviewDO.getDemandId()!=null){
            // 邀约人
            JazMessageDO jazMessageDO1 = new JazMessageDO();
            jazMessageDO1.setMessageType(2);
            jazMessageDO1.setSenderId(interviewDO.getInviteeId());
            jazMessageDO1.setReceiverId(interviewDO.getInviterId());
            jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_201.getMsg());
            jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_201.getNum());
            jazMessageDO1.setDemandId(interviewDO.getDemandId());
            jazMessageDO1.setAuntId(jazUserAuntDO.getId());
            jazMessageDO1.setIsRead(0);
            // 被邀约人
            JazMessageDO jazMessageDO2 = new JazMessageDO();
            jazMessageDO2.setMessageType(2);
            jazMessageDO2.setSenderId(interviewDO.getInviterId());
            jazMessageDO2.setReceiverId(interviewDO.getInviteeId());
            jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_204.getMsg());
            jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_204.getNum());
            jazMessageDO2.setHasSendSms(true);
            jazMessageDO2.setDemandId(interviewDO.getDemandId());
            jazMessageDO2.setIsRead(0);
            jazMessageService.senMessage(jazMessageDO1);
            jazMessageService.senMessage(jazMessageDO2);
        }else {
            // 邀约人
            JazMessageDO jazMessageDO1 = new JazMessageDO();
            jazMessageDO1.setMessageType(2);
            jazMessageDO1.setSenderId(ShiroUtils.getUserId());
            jazMessageDO1.setReceiverId(interviewDO.getInviterId());
            jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_201.getMsg());
            jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_201.getNum());
            jazMessageDO1.setInterviewId(interviewDO.getId());
            jazMessageDO1.setAuntId(jazUserAuntDO.getId());
            jazMessageDO1.setIsRead(0);
            // 被邀约人
            JazMessageDO jazMessageDO2 = new JazMessageDO();
            jazMessageDO2.setMessageType(2);
            jazMessageDO2.setSenderId(ShiroUtils.getUserId());
            jazMessageDO2.setReceiverId(jazInterviewDO.getInviteeId());
            jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_204.getMsg());
            jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_204.getNum());
            jazMessageDO2.setHasSendSms(true);
            jazMessageDO2.setInterviewId(interviewDO.getId());
            jazMessageDO2.setIsRead(0);
            jazMessageService.senMessage(jazMessageDO1);
            jazMessageService.senMessage(jazMessageDO2);
        }
        return update?Result.ok() : Result.fail();
    }

    /**
     * 修改面试结果
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateUserInterview(JazInterviewDO jazInterviewDO) {
        if (jazInterviewDO==null){
            return Result.fail();
        }
        JazInterviewDO interviewDO = selectById(jazInterviewDO.getId());
        if (interviewDO==null){
            return Result.fail();
        }

        // 根据阿姨的邀约状态判断是否需要修改面试结果
        if (interviewDO.getInterStatus()!=1){
            return Result.fail("阿姨未接受面试邀约，不能修改面试结果");
        }

        // 判断面试方式 线下
        //if (interviewDO.getInterviewFormat()==0){
            interviewDO.setResult(jazInterviewDO.getResult());
            if (jazInterviewDO.getResult()==2){
                if (StringUtil.notEmpty(jazInterviewDO.getRejectReasion())){
                    interviewDO.setRejectReasion(jazInterviewDO.getRejectReasion());
                }else {
                    return Result.fail("请输入拒绝原因");
                }
            }
        //}

        // 面试拒绝，存储用户id
        if (jazInterviewDO.getResult()==2){
            // 先判断面试是否拒绝过其他人
            if (StringUtil.notEmpty(interviewDO.getPushUserId())){
                StringBuffer buffer = new StringBuffer();
                String pushUserId = interviewDO.getPushUserId();
                buffer.append(pushUserId).append(",").append(interviewDO.getInviteeId().toString());
                interviewDO.setPushUserId(buffer.toString());
            }else {
                // 没拒绝过其他人
                interviewDO.setPushUserId(interviewDO.getInviteeId().toString());
            }
        }

        // 修改更新时间
        interviewDO.setUpdateTime(new Date());
        // 修改面试状态
        if (interviewDO.getResult()!=0){
            interviewDO.setInterviewStatus(2);
        }
        if (interviewDO.getResult()==1 && interviewDO.getDemandId()!=null) {
            // 将需求进行隐藏
            JazUserDemandDO demandDO = jazUserDemandService.selectById(interviewDO.getDemandId());
            if (demandDO!=null){
                demandDO.setShowStatus(1);
                jazUserDemandService.updateById(demandDO);
            }
        }


        boolean update = updateById(interviewDO);
        EntityWrapper entityWrapper =new EntityWrapper();
        entityWrapper.eq("sys_userid",interviewDO.getInviteeId());
        JazUserAuntDO jazUserAuntDO = jazUserAuntService.selectOne(entityWrapper);
        // 发送消息
        if (interviewDO.getDemandId()!=null){
            // 邀约人
            JazMessageDO jazMessageDO1 = new JazMessageDO();
            jazMessageDO1.setMessageType(2);
            jazMessageDO1.setSenderId(ShiroUtils.getUserId());
            jazMessageDO1.setReceiverId(interviewDO.getInviterId());
            jazMessageDO1.setDemandId(interviewDO.getDemandId());
            jazMessageDO1.setInterviewId(interviewDO.getId());
            jazMessageDO1.setAuntId(jazUserAuntDO.getId());
            jazMessageDO1.setIsRead(0);
            // 被邀约人
            JazMessageDO jazMessageDO2 = new JazMessageDO();
            jazMessageDO2.setMessageType(1);
            jazMessageDO2.setSenderId(ShiroUtils.getUserId());
            jazMessageDO2.setReceiverId(interviewDO.getInviteeId());
            jazMessageDO2.setDemandId(interviewDO.getDemandId());
            jazMessageDO2.setInterviewId(interviewDO.getId());
            jazMessageDO2.setIsRead(0);
            if (interviewDO.getResult()==1) {
                jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_210.getMsg()+"通过，请及时查看!");
                jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_209.getMsg()+"，请及时查看!");
                jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_210.getNum());
                jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_209.getNum());
                jazMessageService.senMessage(jazMessageDO1);
                jazMessageService.senMessage(jazMessageDO2);
            } else if (interviewDO.getResult()==2) {
                jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_210.getMsg()+"拒绝，拒绝原因为"+jazInterviewDO.getRejectReasion()+"，请及时查看!");
                jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_211.getMsg()+"，拒绝原因为"+jazInterviewDO.getRejectReasion()+"，请及时查看!");
                jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_210.getNum());
                jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_211.getNum());
                jazMessageService.senMessage(jazMessageDO1);
                jazMessageService.senMessage(jazMessageDO2);
            }
        }else {
            // 邀约人
            JazMessageDO jazMessageDO1 = new JazMessageDO();
            jazMessageDO1.setMessageType(2);
            jazMessageDO1.setSenderId(ShiroUtils.getUserId());
            jazMessageDO1.setReceiverId(interviewDO.getInviterId());
            jazMessageDO1.setAuntId(jazUserAuntDO.getId());
            jazMessageDO1.setInterviewId(interviewDO.getId());
            jazMessageDO1.setIsRead(0);
            // 被邀约人
            JazMessageDO jazMessageDO2 = new JazMessageDO();
            jazMessageDO2.setMessageType(1);
            jazMessageDO2.setSenderId(ShiroUtils.getUserId());
            jazMessageDO2.setReceiverId(interviewDO.getInviteeId());
            jazMessageDO2.setInterviewId(interviewDO.getId());
            jazMessageDO2.setIsRead(0);
            if (interviewDO.getResult()==1) {
                jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_210.getMsg()+"通过，请及时查看!");
                jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_209.getMsg()+"，请及时查看!");
                jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_210.getNum());
                jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_209.getNum());
                jazMessageService.senMessage(jazMessageDO1);
                jazMessageService.senMessage(jazMessageDO2);
            } else if (interviewDO.getResult()==2) {
                jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_210.getMsg()+"，拒绝原因为"+jazInterviewDO.getRejectReasion()+"，请及时查看!");
                jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_211.getMsg()+"，拒绝原因为"+jazInterviewDO.getRejectReasion()+"，请及时查看!");
                jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_210.getNum());
                jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_211.getNum());
                jazMessageService.senMessage(jazMessageDO1);
                jazMessageService.senMessage(jazMessageDO2);
            }

        }
        return update?Result.ok() : Result.fail();
    }


    /**
     * 用户-取消面试邀约
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class )
    public Result cancelInterview(Long id) {
        if (id == null) {
            return Result.fail();
        }
        // 查询邀约表
        JazInterviewDO jazInterviewDO = this.selectById(id);
        if (jazInterviewDO==null){
            return Result.fail();
        }
        // 判断面试是否通过或拒绝
        if (jazInterviewDO.getResult()==1 || jazInterviewDO.getResult()==2){
            return Result.fail("该阿姨已面试通过或拒绝，不能进行取消邀约");
        }

        // 判断面试是否通过或拒绝
        if (jazInterviewDO.getStatus()==4){
            return Result.fail("该阿姨已拒绝，不能进行修改邀约信息");
        }

        // 设置状态
        jazInterviewDO.setStatus(2);
        jazInterviewDO.setUpdateTime(new Date());

        boolean b = this.updateById(jazInterviewDO);

        // 用户发送消息
        JazMessageDO jazMessageDO1 = new JazMessageDO();
        jazMessageDO1.setMessageType(1);
        jazMessageDO1.setSenderId(ShiroUtils.getUserId());
        jazMessageDO1.setReceiverId(jazInterviewDO.getInviterId());
        jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_218.getMsg());
        jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_218.getNum());
        jazMessageDO1.setHasSendSms(true);
        //jazMessageDO2.setDemandId(jazInterviewDO.getDemandId());
        jazMessageDO1.setInterviewId(jazInterviewDO.getId());
        jazMessageDO1.setIsRead(0);
        // 阿姨发送消息
        JazMessageDO jazMessageDO2 = new JazMessageDO();
        jazMessageDO2.setMessageType(1);
        jazMessageDO2.setSenderId(ShiroUtils.getUserId());
        jazMessageDO2.setReceiverId(jazInterviewDO.getInviteeId());
        jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_218.getMsg());
        jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_218.getNum());
        jazMessageDO2.setHasSendSms(true);
        //jazMessageDO2.setDemandId(jazInterviewDO.getDemandId());
        jazMessageDO2.setInterviewId(jazInterviewDO.getId());
        jazMessageDO2.setIsRead(0);
        jazMessageService.senMessage(jazMessageDO2);
        jazMessageService.senMessage(jazMessageDO1);
        return b?Result.ok():Result.fail();
    }

    /**
     * 用户-修改面试邀约
     *
     * @param jazInterviewDO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class )
    public Result editInterview(JazInterviewDO jazInterviewDO) {
        if (jazInterviewDO==null) {
            return Result.fail();
        }
        // 查询面试邀约数据
        JazInterviewDO interviewDO = this.selectById(jazInterviewDO.getId());
        if (interviewDO==null){
            return Result.fail();
        }
        // 判断面试是否通过或拒绝
        if (interviewDO.getResult()==1 || interviewDO.getResult()==2){
            return Result.fail("该阿姨已面试通过或拒绝，不能进行取消邀约");
        }

        // 判断面试是否通过或拒绝
        if (interviewDO.getStatus()==4){
            return Result.fail("该阿姨已拒绝，不能进行修改邀约信息");
        }

        // 判断邀约的内容是否进行了修改
        Result<String> editStringResult =  getObjectResult(jazInterviewDO, interviewDO);
        if (editStringResult != null) { return editStringResult; }

        // 设置数据
        interviewDO.setInterviewTime(jazInterviewDO.getInterviewTime()); // 面试时间
        interviewDO.setInterviewAddress(jazInterviewDO.getInterviewAddress()); // 地址
        interviewDO.setInterviewAddressDetail(jazInterviewDO.getInterviewAddressDetail()); // 详细地址
        interviewDO.setWorkContent(jazInterviewDO.getWorkContent()); // 工作内容
        interviewDO.setInterviewFormat(jazInterviewDO.getInterviewFormat()); // 面试方式
        interviewDO.setWorkAddress(jazInterviewDO.getWorkAddress()); // 服务地址
        interviewDO.setRemark(jazInterviewDO.getRemark()); // 备注
        interviewDO.setStatus(1); // 备注
        interviewDO.setUpdateTime(new Date());
        interviewDO.setInterStatus(0); // 阿姨待处理
        interviewDO.setInterviewStatus(null);

        boolean b = this.updateById(interviewDO);

        // 用户发送消息
        JazMessageDO jazMessageDO1 = new JazMessageDO();
        jazMessageDO1.setMessageType(1);
        jazMessageDO1.setSenderId(ShiroUtils.getUserId());
        jazMessageDO1.setReceiverId(interviewDO.getInviterId());
        jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_219.getMsg());
        jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_219.getNum());
        jazMessageDO1.setHasSendSms(true);
        //jazMessageDO2.setDemandId(interviewDO.getDemandId());
        jazMessageDO1.setInterviewId(interviewDO.getId());
        jazMessageDO1.setIsRead(0);
        // 阿姨发送消息
        JazMessageDO jazMessageDO2 = new JazMessageDO();
        jazMessageDO2.setMessageType(1);
        jazMessageDO2.setSenderId(ShiroUtils.getUserId());
        jazMessageDO2.setReceiverId(interviewDO.getInviteeId());
        jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_219.getMsg());
        jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_219.getNum());
        jazMessageDO2.setHasSendSms(true);
        //jazMessageDO2.setDemandId(interviewDO.getDemandId());
        jazMessageDO2.setInterviewId(interviewDO.getId());
        jazMessageDO2.setIsRead(0);
        jazMessageService.senMessage(jazMessageDO2);
        jazMessageService.senMessage(jazMessageDO1);

        return  b?Result.ok():Result.fail();
    }

    /**
     * 查询所有的月嫂信息
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UserVO> getUserInterviewAunt(Long id,Long inviterId,String projectId,Long inviteeId) {
        List<UserVO> userVOList = new ArrayList<>();
        // 获取已通过审核的阿姨的信息
        List<JazUserAuntDO> jazUserAuntDOList = jazUserAuntService.selectList(new EntityWrapper<JazUserAuntDO>()
                .eq("audit_status",1) // 审核状态
                .eq("audit_result",0) // 审核结果
                .eq("enable_status",1)); // 是否启用
        // 过滤相应类型的阿姨
        Set<String> projectIdsSet = Arrays.stream(projectId.split(","))
                .map(String::trim)
                .collect(Collectors.toSet());
        List<JazUserAuntDO> filteredList = jazUserAuntDOList.stream()
                // 过滤条件：jazUserAuntDO.getProjectId()中的每一个ID都在projectIdsSet中存在
                .filter(jazUserAuntDO -> {
                    Set<String> auntProjectIdsSet = Arrays.stream(jazUserAuntDO.getProjectId().split(","))
                            .map(String::trim)
                            .collect(Collectors.toSet());
                    return !Collections.disjoint(auntProjectIdsSet, projectIdsSet);  // 只包含一个id就能查出
                    //return projectIdsSet.containsAll(auntProjectIdsSet);  // 包含全部的id才能查出
                })
                .collect(Collectors.toList());
        // 获取已邀请过的阿姨
        List<JazUserAuntDO> userAuntDOList =new ArrayList<>();
        JazInterviewDO jazInterviewDO = selectById(id);
        if (jazInterviewDO == null){
            return null;
        }
        // 面试通过-阿姨通过邀约剔除
        /*if (jazInterviewDO.getInterStatus()==1 && jazInterviewDO.getStatus()==1){
            // 将面试通过和邀约通过的设置进去
            if (StringUtil.notEmpty(jazInterviewDO.getPushUserId())){
                jazInterviewDO.setPushUserId(jazInterviewDO.getPushUserId()+","+String.valueOf(jazInterviewDO.getId()));
            }else {
                jazInterviewDO.setPushUserId(String.valueOf(jazInterviewDO.getId()));
            }
            updateById(jazInterviewDO);
            if (StringUtil.notEmpty(jazInterviewDO.getPushUserId())){
                Set<String> collect = Arrays.stream(jazInterviewDO.getPushUserId().split(","))
                        .map(String::trim)
                        .collect(Collectors.toSet());
                collect.remove(jazInterviewDO.getInviteeId().toString());
                userAuntDOList = filteredList.stream()
                        .filter(jazUserAuntDO -> !collect.contains(jazUserAuntDO.getSysUserid().toString()))
                        .collect(Collectors.toList());
            }
        }*/
        if (jazInterviewDO != null && StringUtil.notEmpty(jazInterviewDO.getPushUserId())){
            Set<String> collect = Arrays.stream(jazInterviewDO.getPushUserId().split(","))
                    .map(String::trim)
                    .collect(Collectors.toSet());
            collect.remove(jazInterviewDO.getInviteeId().toString());
            userAuntDOList = filteredList.stream()
                    .filter(jazUserAuntDO -> !collect.contains(jazUserAuntDO.getSysUserid().toString()))
                    .collect(Collectors.toList());
        }
        // 判断过滤后的阿姨数量
        if (userAuntDOList.size()<=0){ // 未推送过阿姨
            // 获取所有的阿姨的用户表id
            filteredList.stream().forEach(userAunt->{
                UserVO userVO = getUserVO(userAunt);
                userVOList.add(userVO);
            });
        }else {
            // 获取所有的阿姨的用户表id
            userAuntDOList.stream().forEach(userAunt->{
                UserVO userVO = getUserVO(userAunt);
                userVOList.add(userVO);
            });
        }
        return userVOList;
    }

    /**
     * 推送-组装月嫂数据
     * @param userAunt
     * @return
     */
    private UserVO getUserVO(JazUserAuntDO userAunt) {
        UserVO userVO = new UserVO();
        UserDO userDO = userService.selectById(userAunt.getSysUserid());
        userVO.setSex(userDO.getSex());
        if (StringUtil.notEmpty(userDO.getName())){
            userVO.setName(userDO.getName());
        }
        userVO.setSysUserid(userDO.getId());
        if(StringUtil.notEmpty(userDO.getOrderPhone())){
            userVO.setMobile(userDO.getOrderPhone());
        }else {
            userVO.setMobile("");
        }
        // 照片
        userVO.setInchPhoto(userAunt.getInchPhoto());
        userVO.setExperience(userAunt.getExperience()); // 工作经验
        userVO.setWorkExperience(userAunt.getWorkExperience()); // 工作经验
        // 学历
        userVO.setEducation(userDO.getEducation());
        userVO.setAge(userDO.getAge());

        userVO.setProjectId(userAunt.getProjectId());
        if(StringUtil.notEmpty(userAunt.getProjectId())){
            String[] projects = userAunt.getProjectId().split(",");
            List<JazSysProjectDO>  jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id",projects));
            userVO.setProjectName(jazSysProjectDOS.stream().map(JazSysProjectDO::getName).collect(Collectors.joining(",")));
        }
        return userVO;
    }


    /**
     * 后台分页查询面试邀约
     *
     * @param userInterViewQuery
     * @return
     */
    @Override
    public Page<JazInterviewDO> getUserInterviewBackgroundPage(UserInterViewBackGroundQuery userInterViewQuery) {
        Page<JazInterviewDO> page = new Page<>(userInterViewQuery.getPageNumber(),userInterViewQuery.getPageSize());
        // 分页查询数据
        List<JazInterviewDO> jazUserInterview =  jazInterviewDao.selectInterviewBackgroundPage(page,userInterViewQuery);

        // 查询被邀约人信息
        List<Long> userInviteeIds = new ArrayList<>();
        // 查询已经要邀约面试的面试id
        List<Long> interviewIds = new ArrayList<>();
        for (JazInterviewDO jazInterviewDO : jazUserInterview) {
            userInviteeIds.add(jazInterviewDO.getInviteeId());
            if (jazInterviewDO.getId()!=null){
                interviewIds.add(jazInterviewDO.getId());
            }
        }
        if(userInviteeIds.size()<=0){
            return page;
        }
        // 查询用户表
        List<UserDO> userInviteeDOS = userDao.selectList(new EntityWrapper<UserDO>().in("id",userInviteeIds));
        //用户信息不等于null
        if(!ObjectUtils.isEmpty(userInviteeDOS)){
            jazUserInterview.forEach(a -> userInviteeDOS.forEach(b ->{
                if(Objects.equals(a.getInviteeId(),b.getId())){
                    a.setInviteeName(b.getName());
                    a.setInviteePhone(b.getOrderPhone());
                }
            }));
        }

        //查询项目类型
        List<String> projectIds = jazUserInterview.stream().filter(a ->Objects.nonNull(a.getProjectId()))
                .map(JazInterviewDO::getProjectId).distinct().collect(Collectors.toList());
        if(ObjectUtils.isEmpty(projectIds)){
            return page;
        }
        List<String> projectIds1 = new ArrayList<>();
        for (int i = 0; i < projectIds.size(); i++) {
            projectIds1.addAll(Arrays.asList(projectIds.get(i).split(",")));
        }
        List<JazSysProjectDO>  jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id",projectIds1));
        jazUserInterview.forEach(a-> {
            a.setProjectName(jazSysProjectDOS.stream().filter(b -> Objects.nonNull(a.getProjectId())).
                    filter(b -> a.getProjectId().contains(b.getId().toString())).
                    map(JazSysProjectDO::getName).collect(Collectors.joining(",")));
        });

        // 设置分页信息
        page.setRecords(jazUserInterview);

        return page;
    }


    /**
     * 后台面试邀约
     *
     * @param jazInterviewDO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result inviteInterview(JazInterviewDO jazInterviewDO) {
        if (jazInterviewDO==null){
            return Result.fail();
        }
        // 判断该阿姨是否已经邀约过
        JazInterviewDO aDo = this.selectOne(new EntityWrapper<JazInterviewDO>()
                .eq("invitee_id", jazInterviewDO.getInviteeId())
                .eq("demand_id", jazInterviewDO.getDemandId()));
        if (aDo!=null){
            return Result.fail("该阿姨已经邀约过,请勿重复邀约!");
        }

        // 根据需求id，查询邀约过的用户是否有同意面试，或面试通过的
        List<JazInterviewDO> jazInterviewDOList = this.selectList(new EntityWrapper<JazInterviewDO>()
                .eq("demand_id", jazInterviewDO.getDemandId()));
        if (jazInterviewDOList.size()>0){
            for (JazInterviewDO interviewDO : jazInterviewDOList) {
                if (interviewDO.getStatus()!=2){
                    if (interviewDO.getInterStatus()==0){
                        return Result.fail("已邀约过阿姨进行面试,请勿重复邀约!");
                    }else if (interviewDO.getInterStatus()==1 && interviewDO.getInterviewStatus()==1){
                        return Result.fail("已有阿姨进行面试,请勿重复邀约!");
                    }
                    if (interviewDO.getInterStatus()==1 && interviewDO.getResult()==1){
                        // 面试通过，拒绝所有阿姨进行邀约
                        return Result.fail("已有阿姨面试通过,请勿重复邀约!");
                    }
                }
            }
        }

        // 判断结束时间是否小于当前结束时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 获取两个日期的 "yyyy-MM-dd" 格式化的字符串表示
        String endTime1 = simpleDateFormat.format(jazInterviewDO.getInterviewTime());
        String endTime2 = simpleDateFormat.format(new Date());

        // 将格式化的字符串重新解析为 Date 对象
        try {
            Date date1 = simpleDateFormat.parse(endTime1);
            Date date2 = simpleDateFormat.parse(endTime2);

            // 比较两个日期的大小
            if (date1.compareTo(date2) < 0) {
                return Result.fail("面试时间不能小于当前时间");
            }
        } catch (ParseException e) {
            // 处理解析异常
            e.printStackTrace();
            return Result.fail("日期格式错误");
        }



        // 根据需求id和邀约人id查询详细信息
        JazUserDemandDO demandDO = jazUserDemandService.selectById(jazInterviewDO.getDemandId());
        if (demandDO==null){
            return Result.fail();
        }
        JazInterviewDO interviewDO = new JazInterviewDO();
        interviewDO.setProjectId(demandDO.getProjectId().toString());
        interviewDO.setInviterId(demandDO.getSysUserid());
        interviewDO.setInviteeId(jazInterviewDO.getInviteeId());
        interviewDO.setStatus(1);
        interviewDO.setDemandId(demandDO.getId());
        interviewDO.setInterviewTime(jazInterviewDO.getInterviewTime());
        interviewDO.setInterviewFormat(jazInterviewDO.getInterviewFormat());
        interviewDO.setInterviewAddress(jazInterviewDO.getInterviewAddress());
        interviewDO.setInterviewAddressDetail(jazInterviewDO.getInterviewAddressDetail());
        interviewDO.setRemark(jazInterviewDO.getRemark());
        interviewDO.setTitle(demandDO.getTitle());
        interviewDO.setSex(Integer.parseInt(demandDO.getSex()));
        interviewDO.setWorkMoney(demandDO.getSalary());
        interviewDO.setWorkAddress(demandDO.getAddress());
        interviewDO.setWorkTime(demandDO.getWorkDate());
        interviewDO.setEndTime(demandDO.getEndDate());
        interviewDO.setInterviewSource(3);
        interviewDO.setUpdateTime(new Date());

        boolean insert = this.insert(interviewDO);

        if (insert){
            // 邀约成功后，修改中间表
            JazOrderTakingDO jazOrderTakingDO = jazOrderTakingService.selectOne(new EntityWrapper<JazOrderTakingDO>()
                    .eq("demand_id", interviewDO.getDemandId())
                    .eq("invitee_id", interviewDO.getInviteeId()));
            if (jazOrderTakingDO==null){
                return Result.fail();
            }
            // 将邀约id设置到中间表
            jazOrderTakingDO.setInviterId(interviewDO.getInviterId());
            jazOrderTakingDO.setInterviewId(interviewDO.getId());
            jazOrderTakingDO.setInterviewStatus(1);
            jazOrderTakingDO.setUpdateTime(new Date());
            boolean b = jazOrderTakingService.updateById(jazOrderTakingDO);
            if (b==false){
                throw new RuntimeException("修改中间表失败");
            }

            // 被邀约人发送消息
            JazMessageDO jazMessageDO2 = new JazMessageDO();
            jazMessageDO2.setMessageType(2);
            jazMessageDO2.setSenderId(interviewDO.getInviterId());
            jazMessageDO2.setReceiverId(interviewDO.getInviteeId());
            jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_213.getMsg());
            jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_213.getNum());
            jazMessageDO2.setHasSendSms(true);
            jazMessageDO2.setDemandId(interviewDO.getDemandId());
            jazMessageDO2.setInterviewId(interviewDO.getId());
            jazMessageDO2.setIsRead(0);
            jazMessageService.senMessage(jazMessageDO2);
        }else {
            throw new RuntimeException("插入面试邀约失败");
        }

        return Result.ok();
    }


    /**
     * 后台-面试邀约删除
     *
     * @param demandId
     * @param inviteeId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> backgroundDeleted(Long demandId, Long inviteeId) {
        JazOrderTakingDO jazOrderTakingDO = jazOrderTakingService.selectOne(new EntityWrapper<JazOrderTakingDO>()
                .eq("demand_id", demandId)
                .eq("invitee_id", inviteeId));
        if (jazOrderTakingDO==null){
            return Result.fail();
        }
        // 判断是否已经邀约过
        if (jazOrderTakingDO.getInviterId()!=null){
            JazInterviewDO jazInterviewDO = this.selectById(jazOrderTakingDO.getInterviewId());
            if (jazInterviewDO!=null){ // 进行过面试邀约
                if (jazInterviewDO!=null && jazInterviewDO.getResult()==1){
                    return Result.fail("面试已通过，不能删除！");
                } else {
                    // 删除消息  用户找阿姨，没有需求id
                    if (jazInterviewDO.getDemandId() != null) {
                        boolean delete = jazMessageService.delete(new EntityWrapper<JazMessageDO>()
                                .eq("sender_id", jazInterviewDO.getInviterId())
                                .eq("demand_id", jazInterviewDO.getDemandId()));
                        if (delete == false){
                            throw new RuntimeException("删除消息失败");
                        }
                    }
                    boolean b = this.deleteById(jazInterviewDO);
                    if (b==false){
                        throw new RuntimeException("删除面试邀约失败");
                    }
                }
            }
        }
        // 删除临时表数据
        boolean b = jazOrderTakingService.deleteById(jazOrderTakingDO);
        if (b==false){
            throw new RuntimeException("删除面试邀约失败");
        }

        return b?Result.ok():Result.fail();
    }


    /**
     * 后台-查询详情
     *
     * @param demandId
     * @param inviteeId
     * @return
     */
    @Override
    public JazInterviewDO backgroundGetInterview(Long demandId, Long inviteeId) {
        JazOrderTakingDO jazOrderTakingDO = jazOrderTakingService.selectOne(new EntityWrapper<JazOrderTakingDO>()
                .eq("demand_id", demandId)
                .eq("invitee_id", inviteeId));
        if (jazOrderTakingDO==null){
            return null;
        }
        // 获取需求信息
        JazUserDemandDO demandDO = jazUserDemandService.selectById(demandId);
        if (demandDO==null){
            return null;
        }
        JazInterviewDO jazInterviewDO = new JazInterviewDO();
        // 判断是否进行过邀约
        if (jazOrderTakingDO.getInterviewStatus()==1 && jazOrderTakingDO.getInterviewId()!=null){
            // 发起过面试邀约
            JazInterviewDO jazInterviewDO1 = this.selectById(jazOrderTakingDO.getInterviewId());
            if (jazInterviewDO1==null){
                return null;
            }
            jazInterviewDO.setId(jazInterviewDO1.getId());
            jazInterviewDO.setInterviewFormat(jazInterviewDO1.getInterviewFormat());
            jazInterviewDO.setWorkTime(jazInterviewDO1.getWorkTime());
            jazInterviewDO.setEndTime(jazInterviewDO1.getEndTime());
            jazInterviewDO.setInterviewTime(jazInterviewDO1.getInterviewTime());
            StringBuffer append = new StringBuffer().append(jazInterviewDO1.getInterviewAddress()).append(jazInterviewDO1.getInterviewAddressDetail());
            jazInterviewDO.setInterviewAddress(append.toString());
            jazInterviewDO.setStatus(jazInterviewDO1.getStatus());
            jazInterviewDO.setInterviewStatus(jazInterviewDO1.getInterviewStatus());
            jazInterviewDO.setResult(jazInterviewDO1.getResult());
            jazInterviewDO.setRejectReasion(jazInterviewDO1.getRejectReasion());
            jazInterviewDO.setRemark(jazInterviewDO1.getRemark());

            // 判断面试是否成功
            if (jazInterviewDO.getResult()==1){
                UserDO userDO = userDao.selectById(jazOrderTakingDO.getInviteeId());
                if (userDO==null){
                    return null;
                }
                if(StringUtil.notEmpty(userDO.getName())){
                    jazInterviewDO.setInviteeName(userDO.getName());
                }else{
                    jazInterviewDO.setInviteeName(userDO.getUsername());
                }
            }

        }
        // 没发起过面试
        jazInterviewDO.setInviterId(demandDO.getSysUserid());
        jazInterviewDO.setInviterName(demandDO.getUserName());
        jazInterviewDO.setInviterPhone(demandDO.getOrderPhone());
        jazInterviewDO.setCreateTime(jazOrderTakingDO.getCreateTime());
        jazInterviewDO.setTitle(demandDO.getTitle());
        jazInterviewDO.setProjectId(demandDO.getProjectId().toString());
        jazInterviewDO.setSex(Integer.parseInt(demandDO.getSex()));
        jazInterviewDO.setInviteeId(jazOrderTakingDO.getInviteeId());
        jazInterviewDO.setServiceAddress(demandDO.getAddress());
        jazInterviewDO.setWorkContent(demandDO.getRemark());


        //查询阿姨信息
        UserDO userDO = userDao.selectById(jazOrderTakingDO.getInviteeId());
        if (userDO==null){
            return null;
        }
        List<UserVO> list = new ArrayList<>();
        // 循环赋值
        UserVO userVO = new UserVO();
        userVO.setSysUserid(userDO.getId());
        userVO.setName(userDO.getName());
        userVO.setMobile(userDO.getOrderPhone());
        userVO.setSex(userDO.getSex());
        userVO.setAge(userDO.getAge());
        userVO.setEducation(userDO.getEducation());
        // 查询月嫂信息
        JazUserAuntDO jazUserAuntDO = jazUserAuntService.selectOne(new EntityWrapper<JazUserAuntDO>().eq("sys_userid", userDO.getId()));
        if (jazUserAuntDO != null){
            userVO.setWorkExperience(jazUserAuntDO.getWorkExperience());
            userVO.setExperience(jazUserAuntDO.getExperience());
            userVO.setInchPhoto(jazUserAuntDO.getInchPhoto());
            // 查询月嫂类型
            if(StringUtil.notEmpty(jazUserAuntDO.getProjectId())){
                String[] projects = jazUserAuntDO.getProjectId().split(",");
                List<JazSysProjectDO>  jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id",projects));
                userVO.setProjectName(jazSysProjectDOS.stream().map(JazSysProjectDO::getName).collect(Collectors.joining(",")));
            }
        }
        list.add(userVO);
        jazInterviewDO.setUserList(list);

        //查询邀约人项目类型
        if(demandDO.getProjectId()!=null){
            JazSysProjectDO  jazSysProjectDOS = jazSysProjectService.selectById(demandDO.getProjectId());
            jazInterviewDO.setProjectName(jazSysProjectDOS.getName());
        }
        return jazInterviewDO;
    }


    /**
     * 修改面試结果
     *
     * @param jazInterviewDO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result backgroundUpdateInterview(JazInterviewDO jazInterviewDO) {
        if (jazInterviewDO==null){
            return Result.fail();
        }
        JazInterviewDO interviewDO = this.selectOne(new EntityWrapper<JazInterviewDO>()
                .eq("demand_id",jazInterviewDO.getDemandId())
                .eq("invitee_id",jazInterviewDO.getInviteeId()));
        if (interviewDO==null){
            return Result.fail();
        }

        if (interviewDO.getInterStatus()!=1){
            return Result.fail("阿姨未同意邀约，无法修改面试结果");
        }

        // 判断面试方式 线下
        //if (interviewDO.getInterviewFormat()==0){
        interviewDO.setResult(jazInterviewDO.getResult());
        if (interviewDO.getResult()==2){
            if (StringUtil.notEmpty(jazInterviewDO.getRejectReasion())){
                interviewDO.setRejectReasion(jazInterviewDO.getRejectReasion());
            }else {
                return Result.fail("请输入拒绝原因");
            }
        }
        //}

        // 面试拒绝，存储用户id
        if (jazInterviewDO.getResult()==2){
            // 先判断面试是否拒绝过其他人
            if (StringUtil.notEmpty(interviewDO.getPushUserId())){
                StringBuffer buffer = new StringBuffer();
                String pushUserId = interviewDO.getPushUserId();
                buffer.append(pushUserId).append(",").append(interviewDO.getInviteeId().toString());
                interviewDO.setPushUserId(buffer.toString());
            }else {
                // 没拒绝过其他人
                interviewDO.setPushUserId(interviewDO.getInviteeId().toString());
            }
        }
        // 设置更新时间
        interviewDO.setUpdateTime(new Date());

        // 修改面试状态
        if (interviewDO.getResult()!=0){
            interviewDO.setInterviewStatus(2);
        }
        if (interviewDO.getResult()==1 && interviewDO.getDemandId()!=null) {
            // 将需求进行隐藏
            JazUserDemandDO demandDO = jazUserDemandService.selectById(interviewDO.getDemandId());
            if (demandDO!=null){
                demandDO.setShowStatus(1);
                jazUserDemandService.updateById(demandDO);
            }
        }

        boolean update = updateById(interviewDO);
        EntityWrapper entityWrapper =new EntityWrapper();
        entityWrapper.eq("sys_userid",interviewDO.getInviteeId());
        JazUserAuntDO jazUserAuntDO = jazUserAuntService.selectOne(entityWrapper);
        // 发送消息
        if (interviewDO.getDemandId()!=null){
            // 被邀约人
            JazMessageDO jazMessageDO2 = new JazMessageDO();
            jazMessageDO2.setMessageType(1);
            jazMessageDO2.setSenderId(interviewDO.getInviterId());
            jazMessageDO2.setReceiverId(interviewDO.getInviteeId());
            jazMessageDO2.setDemandId(interviewDO.getDemandId());
            jazMessageDO2.setInterviewId(interviewDO.getId());
            jazMessageDO2.setIsRead(0);
           if (interviewDO.getResult()==1) {
                jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_209.getMsg()+"，请及时查看!");
                jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_209.getNum());
               jazMessageService.senMessage(jazMessageDO2);
            } else if (interviewDO.getResult()==2) {
                jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_211.getMsg()+"，拒绝原因为"+jazInterviewDO.getRejectReasion()+"，请及时查看!");
                jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_211.getNum());
               jazMessageService.senMessage(jazMessageDO2);
            }

        }
        return update?Result.ok() : Result.fail();
    }


    /**
     * 取消面试邀约
     *
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result backgroundCancelInterview(Long demandId, Long inviteeId) {
        if (demandId==null || inviteeId==null){
            return Result.fail();
        }
        // 查询是否进行过面试邀约
        JazInterviewDO interviewDO = this.selectOne(new EntityWrapper<JazInterviewDO>()
                .eq("demand_id",demandId)
                .eq("invitee_id",inviteeId));
        if (interviewDO==null){
            return Result.fail("未对该阿姨进行面试邀约，不能进行取消邀约，请先对阿姨进行面试邀约");
        }
        // 判断面试是否通过或拒绝
        if (interviewDO.getResult()==1 || interviewDO.getResult()==2){
            return Result.fail("该阿姨已面试通过或拒绝，不能进行取消邀约");
        }

        // 判断面试是否通过或拒绝
        if (interviewDO.getStatus()==4){
            return Result.fail("该阿姨已拒绝，不能进行取消邀约信息");
        }

        // 阿姨已经同意邀约，不能取消
//        if (interviewDO.getInterStatus()==1){
//            return Result.fail("阿姨已同意邀约，不能进行取消邀约");
//        }

        // 修改邀约临时表记录
//        JazOrderTakingDO jazOrderTakingDO = jazOrderTakingService.selectOne(new EntityWrapper<JazOrderTakingDO>()
//                .eq("demand_id", demandId)
//                .eq("invitee_id", inviteeId));
//        if (jazOrderTakingDO!=null){
//            jazOrderTakingDO.setInterviewId(null);
//            jazOrderTakingDO.setInterviewStatus(0);
//
//            boolean b = jazOrderTakingService.updateById(jazOrderTakingDO);
//            if (!b){
//                throw new NullPointerException();
//            }
//        }
        // 删除邀约消息
//        boolean delete = jazMessageService.delete(new EntityWrapper<JazMessageDO>()
//                .eq("receiver_id", interviewDO.getInviteeId()).eq("interview_id", interviewDO.getId()));
//        if (!delete){
//            throw new NullPointerException();
//        }

        // 发送消息
        // 被邀约人发送消息
        JazMessageDO jazMessageDO2 = new JazMessageDO();
        jazMessageDO2.setMessageType(1);
        jazMessageDO2.setSenderId(ShiroUtils.getUserId());
        jazMessageDO2.setReceiverId(interviewDO.getInviteeId());
        jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_218.getMsg());
        jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_218.getNum());
        jazMessageDO2.setHasSendSms(true);
        jazMessageDO2.setDemandId(interviewDO.getDemandId());
        jazMessageDO2.setInterviewId(interviewDO.getId());
        jazMessageDO2.setIsRead(0);
        jazMessageService.senMessage(jazMessageDO2);

        // 修改邀约表状态
        interviewDO.setStatus(2);
        interviewDO.setInterviewStatus(null);
        interviewDO.setInterStatus(0);
        interviewDO.setResult(0);
        interviewDO.setUpdateTime(new Date());

        boolean b = this.updateById(interviewDO);
        if (!b){
            throw new NullPointerException();
        }

        return b?Result.ok():Result.fail();
    }


    /**
     * 修改面试邀约
     *
     * @param jazInterviewDO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result backgroundEditInterview(JazInterviewDO jazInterviewDO) {
        if (jazInterviewDO==null){
            return Result.fail();
        }
        // 查询是否进行过面试邀约
        JazInterviewDO interviewDO = this.selectOne(new EntityWrapper<JazInterviewDO>()
                .eq("demand_id",jazInterviewDO.getDemandId())
                .eq("invitee_id",jazInterviewDO.getInviteeId()));
        if (interviewDO==null){
            return Result.fail("未对该阿姨进行面试邀约，不能进行修改邀约信息，请先对阿姨进行面试邀约");
        }
        // 判断面试是否通过或拒绝
        if (interviewDO.getResult()==1 || interviewDO.getResult()==2){
            return Result.fail("该阿姨已面试通过或拒绝，不能进行修改邀约信息");
        }
        // 判断面试是否通过或拒绝
        if (interviewDO.getStatus()==4){
            return Result.fail("该阿姨已拒绝，不能进行修改邀约信息");
        }
        // 判断邀约的内容是否进行了修改
        Result<String> editStringResult =  getObjectResult(jazInterviewDO, interviewDO);
        if (editStringResult != null) { return editStringResult; }

        // 设置数据
        interviewDO.setInterviewTime(jazInterviewDO.getInterviewTime()); // 面试时间
        interviewDO.setInterviewAddress(jazInterviewDO.getInterviewAddress()); // 地址
        interviewDO.setInterviewAddressDetail(jazInterviewDO.getInterviewAddressDetail()); // 详细地址
        interviewDO.setWorkContent(jazInterviewDO.getWorkContent()); // 工作内容
        interviewDO.setInterviewFormat(jazInterviewDO.getInterviewFormat()); // 面试方式
        interviewDO.setWorkAddress(jazInterviewDO.getWorkAddress()); // 服务地址
        interviewDO.setRemark(jazInterviewDO.getRemark()); // 备注
        interviewDO.setStatus(1); // 当前状态
        interviewDO.setUpdateTime(new Date());
        interviewDO.setInterStatus(0); // 阿姨待处理
        interviewDO.setInterviewStatus(null);

        boolean b = this.updateById(interviewDO);
        if (!b){
            throw new NullPointerException();
        }

        // 发送消息
        // 被邀约人发送消息
        JazMessageDO jazMessageDO2 = new JazMessageDO();
        jazMessageDO2.setMessageType(1);
        jazMessageDO2.setSenderId(ShiroUtils.getUserId());
        jazMessageDO2.setReceiverId(interviewDO.getInviteeId());
        jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_219.getMsg());
        jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_219.getNum());
        jazMessageDO2.setHasSendSms(true);
        jazMessageDO2.setDemandId(interviewDO.getDemandId());
        jazMessageDO2.setInterviewId(interviewDO.getId());
        jazMessageDO2.setIsRead(0);
        jazMessageService.senMessage(jazMessageDO2);

        return b?Result.ok():Result.fail();
    }

    /**
     * 判断面试邀约信息是否进行了修改
     * @param jazInterviewDO  前端给的
     * @param interviewDO  后端差的
     * @return
     */
    private static Result<String> getObjectResult(JazInterviewDO jazInterviewDO, JazInterviewDO interviewDO) {
        if (interviewDO.getInterviewTime().equals(jazInterviewDO.getInterviewTime()) &&
                interviewDO.getInterviewAddress().equals(jazInterviewDO.getInterviewAddress()) &&
                interviewDO.getInterviewAddressDetail().equals(jazInterviewDO.getInterviewAddressDetail()) &&
                interviewDO.getInterviewFormat().equals(jazInterviewDO.getInterviewFormat()) &&
                areStringsEqual(interviewDO.getRemark(), jazInterviewDO.getRemark())
        ){
            return Result.fail("请修改邀约信息");
        }
        return null;
    }

    public static boolean areStringsEqual(String str1, String str2) {
        if ((str1 == null && str2 == null)
                ||(str1 == "" && str2 == "")
                || (str1 == "" && str2 == null)
                || (str1 == null && str2 == "")) {
            return true;
        }

        if (null!=str1&&str1.equals(str2)) {
            return true;
        }
        return false;
    }
}
